<?php
/* Kammerath Network RSS feed generator that creates 
        and RSS feed with all articles stored for this site */

/* config file for general configs */
require_once("config.php");

/* use the database for queries */
require_once("class/database.class.php");

/* blog class for site ident */
require_once("class/cache.class.php");
require_once("class/blog.class.php");
require_once("class/user.class.php");

/* output gzip enable, when gzip is accepted */
if(array_key_exists("HTTP_ACCEPT_ENCODING", $_SERVER)){
        if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
                // client accepts gzip, so gzip the content
                ob_start("ob_gzhandler");
        }else{ 
                // continue normally when gzip is unsupported
                ob_start();
        }
}

/* create an instance of the blog */
$b = new blog($_SERVER["HTTP_HOST"]);

/* check if the host name requested is the 
 * primary host name and not one of the CDN 
 * host names */
if($_SERVER["HTTP_HOST"]!=CONFIG_CDN_PRIMARY_DOMAIN){
        // 301 redirect and exit, when it is not 
        // the primary domain name and make sure
        // it is redirecting to the same page requested 
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: http://"
                        .CONFIG_CDN_PRIMARY_DOMAIN
                        ."/feed.rss"); 
        
        // exists the this request
        exit();
}

// create the RSS head content
$rss = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n"
                ."<rss version=\"2.0\">\n"
                ."<channel>"
                ."<title>Kammerath Network</title>"
                ."<link>http://".CONFIG_CDN_PRIMARY_DOMAIN."/</link>"
                ."<description>Kammerath Network RSS Feed</description>"
                ."<language>".$b->db->getLanguageCode()."</language>"
                ."<copyright>Jan Kammerath, Kammerath Network</copyright>"
                ."<pubDate>".date("r")."</pubDate>";

$categoryList = $b->db->getArticleCategoryList();
$articleList = $b->getLatestArticles(99999999);
for($a=0; $a<count($articleList);$a++){
        $articleItem = $b->getArticle($articleList[$a]["url"]);
        $text_content = html_entity_decode(strip_tags($articleItem["formatted_body"]));
        $text_content = str_replace("&euro;", "EUR", $text_content);
        $text_content = htmlspecialchars($text_content, ENT_QUOTES, "UTF-8");
        $text_title = html_entity_decode($articleList[$a]["title"]);
        $text_title = str_replace("&", "&amp;", $text_title);

        $rss .= "<item>\n"
                        ."<title>".$text_title."</title>\n"
                        ."<description>".substr($text_content,0,600)."...</description>"
                        ."<link>http://".CONFIG_CDN_PRIMARY_DOMAIN."/".$articleList[$a]["url"].".html</link>"
                        ."<guid>http://".CONFIG_CDN_PRIMARY_DOMAIN."/".$articleList[$a]["url"].".html</guid>"
                        ."<pubDate>".date("r",$articleList[$a]["date_ts"])."</pubDate>"
                        ."</item>\n";
}

// close the rss document
$rss .= "</channel></rss>";

// set the RSS content type header
header("Content-type: application/rss+xml; charset=utf-8");

// output the rss content
print($rss);
?>